var app = getApp()
Page({
  data: {
    imgUrl: "https://tyc-bj.cn/yc/api/attach/",
    currentTab: 0,
    msgList: [
      '111',
      '222',
      '333',
    ],
    orderType: 1, // 1.待服务 2.已完成
    pagenum: 1,
    pagesize: 5,
    info: "",
    orderinfo: [],
    doctorinfo: '',
    pageName: 'index', // 控制tabbar图标
    falseed: false,
    trueed: false,
  },

  onLoad: function(options) {

    this.loadings();
    this.loaddata();
    this.loaddatas();
    this.loadOrder();
    wx.setStorageSync('storeType', 2)
    this.setData({
      url: app.globalData.imgUrl
    })
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    wx.getUserInfo({
      info: "",
      success: function(res) {

        var userInfo = res.userInfo;
        that.setData({
          nickName: userInfo.nickName,
          avatarUrl: userInfo.avatarUrl,
        })
      }
    })
  },
  //手术完成
  accomplish: function(e) {
    var that = this;
    var orderid = e.currentTarget.dataset.orderid;
    wx.request({
      url: app.globalData.baseUrl + 'shopp/yimeiGoodsOrderApi/serviceComplete',
      data: {
        orderid: orderid
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      success: function(t) {
        console.log("t", t);
        if (t.data.data.status_code == 200) {
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 1500
          })
          setTimeout(function() {
            that.onLoad();
          }, 1500)
        }
      }
    })
  },
  loaddatas: function() {
    var that = this;
    var doctorId = wx.getStorageSync('userid');
    // 行业新闻
    wx.request({
      url: app.globalData.baseUrl + 'system/newsapi/getnewsbystate',
      data: {
        pageSize: 10, //每页数量
        pageNum: 1, //页数
        doctorId: doctorId, //医生id
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      method: 'POST',
      success: function(t) {

        that.setData({
          msgList: t.data.data
        })

      }
    })
  },
  loadings: function() {
    var that = this;
    var doctorId = wx.getStorageSync('userid');
    wx.request({
      url: app.globalData.baseUrl + 'doctor/doctor/getDoctorDetail',
      data: {
        'doctorId': '51a32428d9fa441eac2c156c0181e72a' // 修改医生id
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('医生首页详情:', res.data.data);
        wx.hideLoading();

        that.setData({
          // message: res.data.data.goods,
          'doctorinfo': res.data.data
        })
      }
    })
  },
  loaddata: function() {
    var that = this;
    // 获取用户医美订单
    wx.request({
      url: app.globalData.baseUrl + 'shopp/yimeiGoodsOrderApi/getYiMeiAllOrder', //仅为示例，并非真实的接口地址
      data: {
        pagenum: that.data.pagenum, // 页码
        pagesize: that.data.pagesize, //行数
      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8', // 默认值
      },

      success(res) {

      }
    })
  },
  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function(e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {

      if (e.target.dataset.current == 0) {
        that.setData({
          currentTab: e.target.dataset.current,
          orderType: 1,
        })
        that.loadOrder();
      } else {
        that.setData({
          currentTab: e.target.dataset.current,
          orderType: 2,
        })
        that.loadOrder();
      }

    }
    console.log("666", that.data.currentTab);
  },

  // 加载医生端首页订单信息
  loadOrder: function(e) {
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'doctor/doctor/getOrderByDoctor',
      data: {
        state: that.data.orderType,
        pageNo: that.data.pagenum, // 页码
        pageSize: that.data.pagesize, //行数
      },
      method: 'POST',
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8', // 默认值
      },
      success(res) {

        console.log("医生首页订单列表", res)
        that.setData({
          'orderinfo': res.data.data,
        })
      }
    })
  },
  onShareAppMessage: function() {
    return {
      title: '分享好友获取美丽币！',
      path: '/pages/index/index',
      // imageUrl: '/pages/images/aa.jpg',
      success: function() {
        wx.request({
          url: app.globalData.baseUrl + 'doctor/memberTaskApi/addBeauty',
          data: {
            userId: wx.getStorageSync('userid'),
            type: 3
          },
          header: {
            'sign': app.globalData.sign,
            'token': app.globalData.token,
            'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
          },
          method: 'POST',
          success(res) {
            console.log('分享给美丽币给美丽值', res);
          }
        })
      }
    }
  }
})